package com.LeeCode;

/**
 * 袋子里最少数目的球
 */

public class Code1760 {
    public static void main(String[] args) {
        int[] nums = {9};
        int maxOperations = 2;
        System.out.println(new Code1760().minimumSize(nums, maxOperations));
    }

    public int minimumSize(int[] nums, int maxOperations) {
        int left = 0, right = 0;
        for (int num : nums) {
            right = Math.max(right, num);
        }
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(nums, maxOperations, mid))
                right = mid;
            else
                left = mid;
        }
        return right;
    }

    private boolean check(int[] nums, int maxOperations, int limit) {
        int count = 0;
        for (int num : nums) {
            // 隔板法，记录隔板的数量
            count += (num + limit - 1) / limit - 1;
            if (count > maxOperations)
                return false;
        }

        return count <= maxOperations;
    }
}
